package auto.app.service.impl;

import auto.app.controller.from.AutoCollectionResultPage;
import auto.app.exetion.MainExection;
import auto.app.model.AutoCollectionResult;
import auto.app.dao.AutoCollectionResultDao;
import auto.app.model.TestCollection;
import auto.app.service.AutoCollectionResultService;
import auto.app.service.TestCollectionService;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static auto.app.exetion.MainExetionMessage.SELECTCOLLECTERROR;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 杨森
 * @since 2023-01-30
 */
@Service
public class AutoCollectionResultServiceImpl extends ServiceImpl<AutoCollectionResultDao, AutoCollectionResult> implements AutoCollectionResultService {

    @Autowired
    private TestCollectionService testCollectionService;

    @Override
    public AutoCollectionResultPage list(AutoCollectionResultPage autoCollectionResultPage) throws MainExection {
        TestCollection byId = testCollectionService.getById(autoCollectionResultPage.getId());
        if (ObjectUtil.isNull(byId)){
            throw new MainExection(SELECTCOLLECTERROR.getCode(),SELECTCOLLECTERROR.getMsg());
        }
        LambdaQueryWrapper<AutoCollectionResult> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        LambdaQueryWrapper<AutoCollectionResult> last = lambdaQueryWrapper.eq(AutoCollectionResult::getCollectionId, autoCollectionResultPage.getId())
                .eq(ObjectUtil.isNotEmpty(autoCollectionResultPage.getExcuteType()),AutoCollectionResult::getExcuteType,autoCollectionResultPage.getExcuteType())
                .orderByDesc(AutoCollectionResult::getExcuteTime);
        return page(autoCollectionResultPage, last);
    }
}
